class Game < ActiveRecord::Base
	def pair1
		pair1 ||= Pair.find self.pair1_id
	end
	def pair2
		pair2 ||= Pair.find self.pair2_id
	end
	def self.randomize
		pairs = Pair.all
		if pairs.length == 0
			Pair.randomize
			pairs = Pair.all
		end
		pairs = pairs.sort_by{rand}
		while pairs.length > 1
			pair1 = pairs.pop		
			pair2 = pairs.pop
			while pair1.contains_pair(pair2) && pairs.length > 1
				puts "#{pairs.length}"
				pairs = pairs.unshift(pair2)
				pair2 = pairs.pop
			end
			Game.create(:pair1_id => pair1.id, :pair2_id => pair2.id)
			puts "----- #{pairs.length}"
		end
		if pairs.length > 0
			puts "ODD PAIR: #{pairs[0].p1.name} and #{pairs[0].p2.name}"
		end
	end
end

